package com.huaxiafinance.management.controller;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaxiafinance.management.dto.request.QueryMessageModelDto;
import com.huaxiafinance.management.model.MessageModel;
import com.huaxiafinance.management.service.MessageModelService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller("messageModelController")
@RequestMapping("/messageModel")
public class MessageModelController {

  private static final Logger log = Logger.getLogger(MessageModelController.class);

  @Autowired
  private MessageModelService messageModelService;

  @RequestMapping(value = "/queryMessageModel", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryMessageModel(@Valid @ModelAttribute("dto") QueryMessageModelDto dto)
      throws Exception {
    log.info("查询短信模板请求参数" + dto);

    MessageResult messageResult = new MessageResult();

    List<MessageModel> list = messageModelService.queryMessageModel(dto);
    int count = messageModelService.countMessageModel(dto);
    messageResult.setData(list);
    messageResult.setRows(count);
    messageResult.setResultMessage(ServiceCode.SUCCESS);
    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/addMessageModel", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String addMessageModel(HttpServletRequest request) throws Exception {
    String json = request.getParameter("json");

    log.info("新增短息模板请求参数" + json);

    MessageResult messageResult = new MessageResult();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    MessageModel record = mapper.readValue(json, MessageModel.class);

    boolean flag = messageModelService.addMessageModel(record);

    if (flag) {
      messageResult.setResultMessage(ServiceCode.SUCCESS);
    } else {
      messageResult.setFailureResult("名称已有");
    }

    log.info("新增短息模板返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/updateMessageModel", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateMessageModel(HttpServletRequest request) throws Exception {
    String json = request.getParameter("json");

    log.info("修改短息模板请求参数" + json);

    MessageResult messageResult = new MessageResult();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    MessageModel record = mapper.readValue(json, MessageModel.class);

    boolean flag = messageModelService.updateMessageModel(record);

    if (flag) {
      messageResult.setResultMessage(ServiceCode.SUCCESS);
    } else {
      messageResult.setFailureResult("名称已有");
    }

    log.info("修改短息模板返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/beforUpdateMessageModel", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String beforUpdateMessageModel(Integer id) throws Exception {
    log.info("修改短信模板前查询请求参数" + id);

    MessageResult messageResult = new MessageResult();

    MessageModel record = messageModelService.beforUpdateMessageModel(id);

    if (record != null) {
      messageResult.setData(record);
      messageResult.setResultMessage(ServiceCode.SUCCESS);
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    return messageResult.getAsJSON();
  }
}
